//
//  SearchBarBlockVC.m
//  Focus
//
//  Created by 楼主 on 14-11-6.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "SearchBarBlockVC.h"
#import "discoverTitleController.h"
#import "WebColor.h"
@interface SearchBarBlockVC ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property (nonatomic,retain)UISearchBar *searchBar;
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *showArray;

@end

@implementation SearchBarBlockVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.searchArray = [NSMutableArray array];
        self.showArray = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [super dealloc];
    [_searchArray release];
    [_searchBar release];
    [_tableView release];
    [_showArray release];
    [_array release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, -20, 320, 50)];
    self.searchBar.placeholder=@"输入您感兴趣的东西";
    self.searchBar.delegate = self;
    self.searchBar.barStyle = UIBarStyleDefault;
    self.searchBar.showsCancelButton = YES;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeDefault;
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 548) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = self.searchBar;
    [self.view addSubview:_tableView];
    [_searchBar release];
    [_tableView release];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发现" style:UIBarButtonItemStyleBordered target:self action:@selector(buttonClicked:)];
}
#pragma mark button的点击方法
- (void)buttonClicked:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self.showArray removeAllObjects];
    
    for (NSDictionary *dic in self.blockArray) {
        NSString *str = [dic objectForKey:@"title"];
        NSRange range = [str rangeOfString:searchText];
        if (range.length != 0) {
            [self.showArray addObject:str];
            
        }
        [self.tableView reloadData];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.showArray count];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    discoverTitleController *discoverTitle = [[discoverTitleController alloc] init];
    
    for (NSDictionary *dic in self.blockArray) {
        if ([[dic objectForKey:@"title"] isEqualToString:self.showArray[indexPath.row]]) {
            discoverTitle.api_url = [dic objectForKey:@"api_url"];
        }
    }
    [self presentViewController:discoverTitle animated:YES completion:^{
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"]autorelease];
    }
    cell.textLabel.text = [self.showArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
